package cn.com.fssg.platform.base.handler.impl.cl.jiesuan;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @className: Cldyq
 * @description: TODO 代用券
 * @author: xzm
 * @date: 2025/10/6
 **/
@Component
public class Cldyq implements InterfaceHandler {


    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();
//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {


        String id = "";
        String type = jsonObject.getString("type");
        String queryDyqSql = "select id,wt,title,used_count,shenpi_no,shenpi_no_sub,bus_id,history_id,jiesuan_history_id," +
                "jiesuan_id,cjr_name,cjr_idcard,apply_type,org_id,org_name,bind_time,code,month,max_count,service_detail_url," +
                "cjr_hukou,cjr_service_type,cjr_service_cat,plan_type,fuju_id,base_fuju_id,dyq_money,money,invalid,invalid_time," +
                "cjr_sex,cjr_birthday,cjr_type,cjr_cj_level,apply_time,shouli_org,shouli_org_phone,create_by," +
                "create_time,update_by,update_time,remark,version from cl_dyq ";
        Map<String, Object> dyqQueryParam = new HashMap<>();
        Map<String, Object> dyqMap = new HashMap<>();
        List<Map<String,Object>> dyqList = new ArrayList<>();
        if(StringUtils.equals(type, "3")){
            String shenpi_no = jsonObject.getString("shenpi_no");
            String cjr_name = jsonObject.getString("cjr_name");
            dyqQueryParam.put("shenpi_no", shenpi_no);
            dyqQueryParam.put("cjr_name", cjr_name);
            String queryBindDyqSql = "select id,wt,title,used_count,shenpi_no,shenpi_no_sub,bus_id,history_id,jiesuan_history_id," +
                    "jiesuan_id,cjr_name,cjr_idcard,apply_type,org_id,org_name,bind_time,code,month,max_count,service_detail_url," +
                    "cjr_hukou,cjr_service_type,cjr_service_cat,plan_type,fuju_id,base_fuju_id,dyq_money,money,invalid,invalid_time," +
                    "cjr_sex,cjr_birthday,cjr_type,cjr_cj_level,apply_time,shouli_org,shouli_org_phone,create_by," +
                    "create_time,update_by,update_time,remark,version," +
                    "CASE WHEN jiesuan_id IS NULL OR jiesuan_id = '' THEN '未绑定' ELSE '已绑定' END AS bind_status " +
                    "from cl_dyq where shenpi_no = :shenpi_no and cjr_name = :cjr_name";
            dyqList = jdbcTemplate.queryForList(queryBindDyqSql, dyqQueryParam);
        }else if (StringUtils.equals(type, "4")) {
            //通过代用券id获取机构及机构打卡地址
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT dyq.id,dyq.cjr_name,dyq.cjr_idcard,dyq.org_id,org.org_name,org.org_address FROM cl_dyq dyq LEFT JOIN cl_org org ON dyq.org_id = org.id");
            sql.append(" where dyq.id =:id");
            id = jsonObject.getString("id");
            dyqQueryParam.put("id", id);
            dyqMap = jdbcTemplate.queryForMap(sql.toString(), dyqQueryParam);
            return dyqMap;
        }else{
            id = jsonObject.getString("id");
            dyqQueryParam.put("id", id);
            queryDyqSql += "where id = :id";
            dyqMap = jdbcTemplate.queryForMap(queryDyqSql, dyqQueryParam);
        }

        if (StringUtils.equals(type, "0")) {
            //白名单
            String wt = jsonObject.getString("wt");
            if (StringUtils.isNotBlank(id)) {
                wt = StringUtils.isBlank(wt) ? "0" : wt;
                dyqMap.put("wt", wt);
                String dyqUpdateUserSql = "update cl_dyq set wt = :wt  where id = :id";
                int count = jdbcTemplate.update(dyqUpdateUserSql, dyqMap);
                return count;
            }
        } else if (StringUtils.equals(type, "1")) {
            //解绑
            //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            if (StringUtils.isNotBlank(id)) {

                Optional<Object> valueOpt = Optional.ofNullable(dyqMap.get("jiesuan_id"));
                if (valueOpt.isPresent()) {
                    //有值
                    Map<String,Object> jsQueryMap  = new HashMap<>();
                    Map<String,Object> jsMap  = new HashMap<>();
                    jsQueryMap.put("id",id);
                    String dljsSql = "select * from cl_apply_xunlian_org_jiesuan where id = :id";
                    jsMap = jdbcTemplate.queryForMap(dljsSql, jsQueryMap);
                    //判断是否已经审核或办结
                    Optional<Object> valueStatus = Optional.ofNullable(jsMap.get("flow_status"));
                    if(valueStatus.isPresent()){
                        String flow_status = (String) jsMap.get("flow_status");
                        if(StringUtils.equals(flow_status, "00")){
                            Map<String,Object> map  = new HashMap<>();
                            map.put("jiesuan_id","");
                            map.put("org_name","");
                            map.put("org_id","");
                            map.put("bind_time","");
                            map.put("plan_type","");
                            String dyqUpdateUserSql = "update cl_dyq set jiesuan_id = :jiesuan_id,org_name = :org_name,org_id = :org_id,bind_time = :bind_time,plan_type = :plan_type  where id = :id";
                            int count = jdbcTemplate.update(dyqUpdateUserSql, map);
                            return count;
                        }else{
                            return new Exception("该代用券已使用并结算");
                        }
                    }
                }else{
                    //没值
                    return new Exception("该代用券未绑定");
                }
            }
        } else if (StringUtils.equals(type, "2")) {
            return dyqMap;
        }else if (StringUtils.equals(type, "3")) {
            return dyqList;
        }
        return null;
    }
}